// 安装vuex依赖包——> 导入vuex包——> 创建store对象 ——>将store对象挂载到vue实例中
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import jsonp from 'jsonp'
// 表单验证的规则
import {
  stripscript,
  validatePass,
  validateEmail,
  validateVCode
} from "../utils/validate";
export default new Vuex.Store({
  // state提供唯一数据源，存放的就是全局共享的数据
  state: {
    // 接收验证表单传过来的形参
    obj:{},
    login:0,
    account:{},
    szzsData:{},
    szczData:{},
    hs300Data: {},
    upList:[],
    downList:[],
    industryAllList:[],
    industryUpList: [],
    industryDownList: [],
    conceptAllList: [],
    conceptUpList:[],
    conceptDownList: [],
    areaAllList: [],
    areaUpList:[],
    areaDownList: [],
    sAUpList: [],
    sADownList: [],
    sA5MinUpList: [],
    sA5MinDownList: [],
    sATurnoverList: [],
    sAHSList: [],
    sALBList:[],
    sAZFList: [],
    sBUpList: [],
    sBDownList: [],
    sB5MinUpList: [],
    sB5MinDownList: [],
    sBTurnoverList: [],
    sBHSList: [],
    sBLBList:[],
    sBZFList: [],
    indexCode:0,
    stockCode:0,
    indexType:'',
    plateType:'',
    stockList:{},
    hotStocksList:[]
  },
  // 用于变更store里面的数据，不可以直接操作。可以集中监控所有数据的变化
  mutations: {
    setLoginAccount(state,account) {
      state.login = 1;
      state.account = account;
      // localStorage.setItem('login', state.login); //本地存储
      // localStorage.setItem('account', state.account); //本地存储
    },
    clearLoginAccount(state){
      state.login = 0;
      state.account = {};
    },
    // 沪深行情页面
    setHSMainData(state, res) {
      state.szzsData=res['0000001'];
      console.log(state.szzsData);
      state.szczData=res['1399001'];
      console.log(state.szczData);
      state.hs300Data=res['1399300'];
      console.log(state.hs300Data);
      console.log('***********************************************************');
      state.industryAllList = res.RANK_SC_PLATE_HANGYE_ALL.list;
      console.log(state.industryAllList);
      state.industryUpList = res.RANK_SC_PLATE_HANGYE_UP.list;
      console.log(state.industryUpList);
      state.industryDownList = res.RANK_SC_PLATE_HANGYE_DOWN.list;
      console.log(state.industryDownList);

      state.conceptAllList = res.RANK_SC_PLATE_GAINIAN_ALL.list;
      console.log(state.conceptAllList);
      state.conceptUpList = res.RANK_SC_PLATE_GAINIAN_UP.list;
      console.log(state.conceptUpList);
      state.conceptDownList = res.RANK_SC_PLATE_GAINIAN_DOWN.list;
      console.log(state.conceptDownList);

      // state.areaAllList = res.RANK_SC_PLATE_DIYU_ALL.list;
      // console.log(state.areaAllList);
      state.areaUpList = res.RANK_SC_PLATE_DIYU_UP.list;
      console.log(state.areaUpList);
      state.areaDownList = res.RANK_SC_PLATE_DIYU_DOWN.list;
      console.log(state.areaDownList);
    },
    setmIndustryData(state) {
      state.upList = state.industryUpList;
      state.downList = state.industryDownList;
    },
    setmConceptData(state) {
      state.upList = state.conceptUpList;
      state.downList = state.conceptDownList;
    },
    setmAreaData(state) {
      state.upList = state.areaUpList;
      state.downList = state.areaDownList;
    },
    setStockMainData(state, res) {
      state.sAUpList = res.RANK_AUP.list;
      console.log(state.sAUpList);
      state.sADownList = res.RANK_ADOWN.list;
      state.sA5MinUpList = res.RANK_A5MINUP.list;
      state.sA5MinDownList = res.RANK_A5MINDOWN.list;
      state.sATurnoverList = res.RANK_ATURNOVERUP.list;
      state.sAHSList = res.RANK_AHSUP.list;
      state.sALBList = res.RANK_AVOLUMECHGUP.list;
      state.sAZFList = res.RANK_AZFUP.list;
    },
    // 待
    setsBGuData(state) {
      
    },
    // 个股页面
    setIndexCode(state, indexCode) {
      console.log(indexCode);
      state.indexCode = indexCode;
      console.log(state.indexCode);
    },
    setIndexType(state, indexType) {
      console.log(indexType);
      
      state.indexType = indexType;
      console.log(state.indexType);
    },
    // 板块页面
    setPlateType(state, plateType) {
      console.log(plateType);
      
      state.plateType = plateType;
      console.log(state.plateType);
    },
    setStockCode(state,stockID){
      state.stockID = stockID;
      console.log(state.stockID);
    },
    setStockData(state, res) {
      state.stockList=res
    },
    // 设置验证规则的参数的参数
    setValidateParam(state, obj) {
      state.obj = obj;
    },
    // set hotStock
    setHotStocksList(state, res) {
      state.hotStocksList=res.isStockData.data.hotStocks;
      
    }
  },
  // 处理异步函数
  actions: {
    // 行情中心的中心板块数据：三大盘指数及行业概览等
    getHSMain(context){
      jsonp('http://api.money.126.net/data/feed/RANK_SC_PLATE_HANGYE_ALL,RANK_SC_PLATE_HANGYE_UP,RANK_SC_PLATE_HANGYE_DOWN,RANK_SC_PLATE_GAINIAN_ALL,0000001,1399001,1399300,HSRANK_COUNT_SHA,HSRANK_COUNT_SZA,HSRANK_COUNT_SH3,RANK_SC_PLATE_GAINIAN_UP,RANK_SC_PLATE_GAINIAN_DOWN,RANK_SC_PLATE_DIYU_UP,RANK_SC_PLATE_DIYU_DOWN,RANK_AUP,RANK_A5MINUP,RANK_ADOWN,RANK_A5MINDOWN,RANK_ATURNOVERUP,RANK_AHSUP,RANK_AVOLUMECHGUP,RANK_AZFUP,RANK_KCBUP,RANK_KCB5MINUP,RANK_KCBDOWN,RANK_KCB5MINDOWN,RANK_KCBCJL,RANK_KCBHS,RANK_KCBLB,RANK_KCBZF,RANK_ZXBUP,RANK_ZXB5MINUP,RANK_ZXBDOWN,RANK_ZXB5MINDOWN,RANK_ZXBCJL,RANK_ZXBHS,RANK_ZXBLB,RANK_ZXBZF,RANK_CYBUP,RANK_CYB5MINUP,RANK_CYBDOWN,RANK_CYB5MINDOWN,RANK_CYBCJL,RANK_CYBHS,RANK_CYBLB,RANK_CYBZF,RANK_BUP,RANK_B5MINUP,RANK_BDOWN,RANK_B5MINDOWN,RANK_BTURNOVERUP,RANK_BHSUP,RANK_BVOLUMECHGUP,RANK_BZFUP,TING_PAI_TI_SHI_RANK,XIN_GU_SHANG_SHI_RANK,XIAN_SHOU_JIE_JIN_RANK,FEN_HONG_SONG_GU_RANK,RANK_SC_GONG_GAO',{},(err,res)=>{
        if(!err){
          console.log(res);
          
          /* this.szzsData=res['0000001'];
          console.log(this.szzsData);
          this.szczData=res['1399001'];
          console.log(this.szczData);
          this.hs300Data=res['1399300'];
          console.log(this.hs300Data); */
          context.commit('setHSMainData', res)
          context.commit('setmIndustryData')

          context.commit('setStockMainData', res)

        }
      });
    },
    // 个股详情
    selectStock(context,code) {
      console.log(this.state.indexCode);
      jsonp('https://api.money.126.net/data/feed/'+code+',money.api', {}, (err, res) => {
        if (!err) {
          console.log(res);
          context.commit('setStockData', res)
        }
      });
    },
    getLoginAccount(context,account) {
      context.commit('setLoginAccount',account)
      console.log("setLogin");
      
      // localStorage.setItem('login', state.login); //本地存储
      // localStorage.setItem('account', state.account); //本地存储
    },
    // 验证表单
    validateEmail(context,obj){
      // ERROR!!! callback undefined 先将形参存在state中
      context.commit('setValidateParam', obj)
      if (obj.value === "") {
        obj.callback(new Error("请输入用户名(邮箱)"));
      } else if (validateEmail(obj.value)) {
        obj.callback(new Error("用户名格式有误"));
      } else {
        obj.callback(); //true
      }
    },
    validatePassword(context,obj){
      // 过滤后的数据
      /* this.loginForm.password = stripscript(value);
      value = this.loginForm.password; */
      context.commit('setValidateParam', obj)
      if (stripscript(obj.value) === "") {
        obj.callback(new Error("请输入密码"));
      } else if (validatePass(stripscript(obj.value))) {
        obj.callback(new Error("密码为6至20位数字+字母"));
      } else {
        obj.callback();
      }
    },
    validatePasswords(context,obj){
      // 如果模块值为login, 直接通过
      /* if (model.value === "login") {
        callback();
      } */
      // 特殊字符过滤后的数据
      /* this.loginForm.passwords = stripscript(value);
      value = this.loginForm.passwords; */
      /* if (stripscript(value) === "") {
        callback(new Error("请再次输入密码"));
      } else if (stripscript(value) != this.modifyPwdForm.password) {
        callback(new Error("重复密码不正确"));
      } else {
        callback();
      } */
    },
    validateCode(context,obj){
      context.commit('setValidateParam', obj)
      if (obj.value === "") {
        return obj.callback(new Error("请输入验证码"));
      } else if (validateVCode(obj.value)) {
        return obj.callback(new Error("验证码格式有误"));
      } else {
        obj.callback();
      }
    },
    validatePhone(context, obj) {
      context.commit('setValidateParam', obj)
      if (obj.value === "") {
        return obj.callback(new Error("请输入电话号码"));
      } else if (validateVCode(obj.value)) {
        return obj.callback(new Error("电话号码格式有误"));
      } else {
        obj.callback();
      }
    },
    validateIdNo(context, obj) {
      context.commit('setValidateParam', obj)
      if (obj.value === "") {
        return obj.callback(new Error("请输入身份证号"));
      } else if (validateVCode(obj.value)) {
        return obj.callback(new Error("身份证号格式有误"));
      } else {
        obj.callback();
      }
    },
    // hotStock
    hotStock(context) {
      console.log("*************************hotStock*************************")
      
      jsonp('https://i.money.163.com/hs/api/myStockList.json',{},(err,res)=>{
        if(!err){

          console.log("*************************ReshotStock*************************")

          console.log(res);
          /* if(this.isLogin===1){
            // 结合网易财经的登录与未登录,但是有一个问题是请求结束之后isStockData就无法使用，显然是错误的
            // 所以需要在全局下定义这个对象，一打开网站就应该调用isLogin()方法，（isLogin函数返回一个isLogin变量？）从而修改这个isStockData对象。
            // 而且如果登录应该生成一个有效token（前端根据这个token判断是否是登录状态，如果不是登录状态，需要用户重新登录
            console.log(this.isStockData);
            this.isStockData.success=1;
            console.log(this.isStockData);
            // 往this.isStock.data对象里面加入一个myStocks数组？
          }else{ */
            // 未登录：返回对象：success:0,errorType:-1,data:{},msg:{}
            console.log("*************************setHotStocksList*************************")
            context.commit('setHotStocksList', res)
            console.log("*************************热门股票*************************")
            
          // }
        }
      });
    },
  },
  // Gutter：对store中的数据进行加工处理形成新的数据
  modules: {
  }
})
